﻿using System;
using Microsoft.VisualStudio.Utilities;
using Microsoft.VisualStudio.Text.Tagging;
using System.ComponentModel.Composition;
using Microsoft.VisualStudio.Shell;
using Microsoft.VisualStudio.TextManager.Interop;
using Microsoft.VisualStudio.Text;


namespace Marias.Extensions.CSharp {
    [Export(typeof(ITaggerProvider))]
    [TagType(typeof(IOutliningRegionTag))]
    [ContentType("CSharp")]
    internal sealed class StructureTaggerProvider : ITaggerProvider {

        [Import]
        internal SVsServiceProvider _ServiceProvider = null;
        [Import]
        internal ITextDocumentFactoryService _TextDocService = null;

        internal StructureTaggerProvider() {
        }

        public ITagger<T> CreateTagger<T>(ITextBuffer buffer) where T : ITag {

            Type i_type = Type.GetTypeFromCLSID(new Guid("694DD9B6-B865-4C5B-AD85-86356E9C88DC"));
            
            if (_ServiceProvider != null)
            {
                IVsContainedLanguageFactory i_fact = (IVsContainedLanguageFactory)_ServiceProvider.GetService(i_type);
            } else
                return null;
            ITextDocument i_doc;
            if (_TextDocService.TryGetTextDocument(buffer, out i_doc))
            {
                Func<ITagger<T>> i_tagger = delegate() { return new StructureTagger(buffer, i_doc.FilePath) as ITagger<T>; };
                return buffer.Properties.GetOrCreateSingletonProperty<ITagger<T>>(i_tagger);
            } else
                return null;
        }
    }
}
